package com.shop.user.listener;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.shop.user.bean.ShortMsgBean;
import com.shop.user.test.SmsUtil;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.util.List;

/***
 *
 * @Author:jackhu
 * @Description:jackhu
 * @date: 2019/3/19 18:02
 *  消息监听对象
 ****/
public class ShortMessageListener implements MessageListenerConcurrently {

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(
            List<MessageExt> msgList, ConsumeConcurrentlyContext context) {
        try {
            for (MessageExt msg : msgList) {
                // 获取消息体
                String body = new String(msg.getBody(), RemotingHelper.DEFAULT_CHARSET);
                ShortMsgBean shortMsgBean = JSON.parseObject(body, ShortMsgBean.class);

                // 发送短信
                SendSmsResponse response = SmsUtil.sendSms(shortMsgBean.getMobile(), shortMsgBean.getTemplateParam());
                System.out.println(response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return  ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }

        return  ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
}
